<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //发送ajax请求，返回的结果是promise对象
        function sendAJAX(url) {
            return new Promise((resolve, reject) => {
                //1.创建对象
                const x = new XMLHttpRequest()
                    //2.初始化
                x.open('GET', url)
                    //3.发送
                x.send()
                    //4.事件绑定
                x.onreadystatechange = function() {
                    if (x.readyState === 4) {
                        if (x.status >= 200 && x.status < 300) {
                            //成功啦
                            resolve(x.response)
                        } else {
                            //失败了
                            reject(x.status)
                        }
                    }
                }
            })
        }
        //promise then  方法测试
        // sendAJAX('https://api.apiopen.top/getJoke').then(value => {
        //     console.log(result);
        // }, reason => {
        //     console.log(reason);
        // })

        //async 和await 测试   （*ajax 可以替换为axios发送请求;用await接收结果*）
        async function main() {
            let result = await sendAJAX('https://api.apiopen.top/getJoke');
            let tianqi = await sendAJAX('')
        }
        main();
    </script>
</body>

</html>